import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { HeroService } from '../../../../services/hero.service';
import { NzMessageService,NzModalService } from 'ng-zorro-antd';
import {
  FormBuilder,
  FormControl,
  FormGroup,
  Validators
} from '@angular/forms';
import { options } from '../../../../common/areaoption';

@Component({
  selector: 'app-addtenant',
  templateUrl: './addtenant.component.html',
  styleUrls: ['./addtenant.component.css']
})
export class AddtenantComponent implements OnInit {

  validateForm: FormGroup;
  btn_loading=false;
  public selectArea:number;
  public nzOptions = options;
  public values: any[] = ['请选择地区'];
  public onChanges(values: any): void {
    console.log(this.values);
    if(this.values[1]){
      this.selectArea=0;
    }
    if(this.values.length==0){
      this.selectArea=-1;
    }
  }

  submitForm(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[ i ].markAsDirty();
      this.validateForm.controls[ i ].updateValueAndValidity();
    }
  }

  constructor(private heroService: HeroService,
              private fb: FormBuilder,
              private router: Router,
              private message: NzMessageService,
              private modalService: NzModalService) { }

  phoneNUmberValidator = (control: FormControl): { [ s: string ]: boolean } => {
    if (!control.value) {
      return { required: true };
    } else if (!(/^1\d{10}$/.test(control.value))) {
      return { confirm: true, error: true };
    }
  }

  ngOnInit(): void {
    this.validateForm = this.fb.group({
      companyName: [ null, [ Validators.required ] ],
      name: [ null, [ Validators.required ] ],
      tel: [ null, [ Validators.required ] ],
      phoneNumberPrefix: [ '+86' ],
      phoneNumber: [ null, [ Validators.required ] ],
      address: [ null, [ Validators.required ] ]
    });
  }

  addTenant() {
    if(this.values[0] =="请选择地区"){
      this.selectArea=-1;
    }
    if(this.validateForm.status=="VALID" && this.selectArea !=-1){
        this.btn_loading=true;
        if(!this.values[2]){
          this.values[2]=-1;
        }
        if(!this.values[1]){
          this.values[1]=-1;
        }
        let url=host+"/main/merchantConnectUser/save";
        let model='companyName='+this.validateForm.value.companyName+'&tel='+this.validateForm.value.tel+'&name='+this.validateForm.value.name+'&address='+this.validateForm.value.address+'&provinceId='+this.values[0]+'&cityId='+this.values[1]
        +'&districtId='+this.values[2]+'&mobile='+this.validateForm.value.phoneNumber;
        this.heroService.post(url,model).then(result=>{
            this.btn_loading=false;
            if(result.code==0){
              this.message.success('添加成功');
              this.router.navigate(['/main/tenantlist']);
            }else{
              this.modalService.warning({
                nzTitle: '提示',
                nzContent: result.msg
              });
            }
        })
    }
  }

  checkMobile(){
    this.validateForm.get('phoneNumber')!.setValidators(this.phoneNUmberValidator);
    this.validateForm.get('phoneNumber')!.markAsDirty();
    this.validateForm.get('phoneNumber')!.updateValueAndValidity();
  }
  clearCheckMobile(){
    this.validateForm.get('phoneNumber')!.clearValidators();
    this.validateForm.get('phoneNumber')!.markAsPristine();
    this.validateForm.get('phoneNumber')!.updateValueAndValidity();
  }
  backPrevent(){
    return false;
  }
}
